<?php
namespace app\admin\controller;
class Paycard extends Common
{
    //共享网址
    public function index()
    {   
        if(request() -> isPost()){
            $key = input('post.key');
            $status = input('post.status');
            $page = input('page')?input('page'):1;
            $pageSize =input('limit')?input('limit'):10;
            if($status){     
                $list = db('common_card')
                     -> where('id','like','%'.$key.'%')
                     -> where('status',$status)
                     -> order('dateline desc')
                     -> paginate(array('list_rows'=>$pageSize,'page'=>$page))
                     -> toArray();  
            }else{
                 $list = db('common_card')
                 -> where('id','like','%'.$key.'%')
                 -> order('dateline desc')
                 -> paginate(array('list_rows'=>$pageSize,'page'=>$page))
                 -> toArray();  
            }
            foreach ($list['data'] as $key => $value) {
                $name = $this->urlid($value['typeid']);
                $list['data'][$key]['typename'] =  $name;
                $username = $this->urlidm($value['uid']);
                $list['data'][$key]['username'] =   $username;
                $usernames = $this->urlidm($value['makeruid']);
                $list['data'][$key]['usernames'] =   $usernames;
                $list['data'][$key]['useddateline'] =   date('Y-m-d H:i',$value['useddateline']);
                $list['data'][$key]['cleardateline'] =   date('Y-m-d',$value['cleardateline']);
                $list['data'][$key]['dateline'] =   date('Y-m-d H:i',$value['dateline']);
            }
            return json(['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1]);
        }
        return $this->fetch();
    }

    public function urlid($id){
        $cate = db('common_card_type')->field('id,typename')->select();  
        foreach ($cate as $k => $v){
            if($v['id'] == $id){
              return $v['typename'];
            }
        }
    }

    public function urlidm($id){
        $cate = db('common_member')->field('uid,username')->select();  
        foreach ($cate as $k => $v){
            if($v['uid'] == $id){
              return $v['username'];
            }
        }
    }

    //添加共享网址分类
    public function add()
    {   
        if(request() ->isPost()){
            set_time_limit(0);
            $rands = rand(4,16);
            $num = trim(input('post.num'));
            for ($i=0; $i <$num ; $i++) { 
                $data['id'] = randoms($rands);
                $data['typeid'] = input('post.typeid');
                $data['cleardateline'] = strtotime(trim(input('post.cleardateline')));
                $data['dateline'] = time();
                $data['makeruid'] = session('uid');
                $data['day'] = trim(input('post.day'));
                $info = db('common_card')->where($data)->select();
                if(!$info){        
                    $cate = db('common_card')->insert($data);
                }
            }
        return json(['code'=>1,'msg'=>'生成成功','url' => url('paycard/index')]);
        }else{    
           $list = db('common_card_type')->field('id,typename')->select();
           $this->assign('list',$list);
           return $this->fetch();
        }
    }

    //删除单条记录(删除数据)
    public function del()
    {   
        $data['id'] = input('param.id');  
        if($data){
            $del = db('common_card')->where($data)->delete();
            if($del){
                return json(['code'=>1,'msg'=>'删除成功']);
            }else{
                return json(['code'=>0,'msg'=>'删除失败']);
            }
        }
        
    }

    //删除单条记录(删除数据)
    public function delall()
    {   
        if(request() -> isPost()){
            $ids = $_POST['ids'];  
            $all = db('common_card')->delete($ids);
            if($all){
                return json(['code'=>1,'msg'=>'删除成功']);
            }else{
                return json(['code'=>0,'msg'=>'删除失败']);
            }
        }
    }
}